import store from '@/store/index.js'
export default function request(action, data=null, {
	functionName = 'user-id-files',
	showModal = true
} = {}) {
	return uniCloud.callFunction({
		name: functionName,
		data: {
			action,
			data
		}
	}).then(({
		result
	}) => {
		if (!result) {
			return Promise.resolve(result)
		}
		if (result.code) {
			const err = new Error(result.message)
			err.code = result.code
			return Promise.reject(err)
		}
		const {
			token,
			tokenExpired
		} = result
		if (token && tokenExpired) {
			store.commit('chat/setUserInfo',{token,tokenExpired})
			uni.setStorageSync('uni_id_token', token)
			uni.setStorageSync('uni_id_token_expired', tokenExpired)
		}
		return Promise.resolve(result)
	}).catch(err => {
		showModal && uni.showModal({
			content: err.message || '请求服务失败',
			showCancel: false
		})
		return Promise.reject(err)
	})
}


